package com.gk.panda.apis.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.panda.apis.entity.TransectManager.dto.TransectFileDto;
import com.gk.panda.apis.entity.TransectManager.dto.TransectListDto;
import com.gk.panda.apis.entity.TransectManager.entity.TransectEntity;
import com.gk.panda.apis.entity.TransectManager.vo.PointDataVo;
import com.gk.panda.apis.entity.TransectManager.vo.TransectDetailsOfAppVo;
import com.gk.panda.apis.entity.TransectManager.vo.TransectFileVo;
import com.gk.panda.apis.entity.TransectManager.vo.TransectListVo;
import com.gk.panda.apis.mapper.TransectManagerMapper;
import com.gk.panda.apis.service.TransectManagerService;
import com.gk.panda.apis.service.TransectPointDataService;
import com.gk.panda.commons.constant.Constants;
import com.gk.panda.commons.exception.ServerException;
import com.gk.panda.commons.util.UserUtil;
import com.gk.panda.pojo.TransectManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: TransectServiceImpl
 * @Description: 样线管理
 * @author: Sir.yang
 * @date: 2022/11/9 15:44
 * @version: 1.0
 **/
@Service
@Slf4j
public class TransectManagerServiceImpl extends ServiceImpl<TransectManagerMapper, TransectManager> implements TransectManagerService {

    @Autowired
    private TransectPointDataService transectPointDataService;

    @Autowired
    private TransectManagerMapper transectManagerMapper;

    /**
     * ------------------APP-------------------------------
     **/

    /**
     * @description: APP获取样线列表
     * @param: []
     * @return: java.util.List<java.lang.String>
     * @date: 2022/11/9 18:10
     * @version: 1.0
     **/
    @Override
    public List<String> getTransectListOfApp(Integer fkOrgId) {
        List<String> list = new ArrayList<>();
        QueryWrapper<TransectManager> wrapper = new QueryWrapper<>();
        wrapper.select("transect_name");
        wrapper.eq("fk_org_id", fkOrgId);
        wrapper.eq("del_flag", Constants.ZERO);
        wrapper.eq("transect_status", Constants.ZERO);
        List<TransectManager> transectManagers = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(transectManagers)){
            list = transectManagers.stream().map(TransectManager::getTransectName).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * @description: APP获取样线的详情
     * @param: []
     * @return: com.gk.panda.apis.entity.TransectManager.vo.TransectDetailsOfAppVo
     * @date: 2022/11/9 18:10
     * @version: 1.0
     **/
    @Override
    public TransectDetailsOfAppVo getTransectDetailsOfApp(Integer transectId) {
        TransectDetailsOfAppVo vo = new TransectDetailsOfAppVo();
        //获取样线信息
        TransectManager manager = baseMapper.selectById(transectId);
        if(ObjectUtil.isNotEmpty(manager)){
            BeanUtils.copyProperties(manager, vo);
        }
        //获取样线实际数据经纬度
        List<PointDataVo> pointDataVos = transectPointDataService.getPointDataByTransectId(transectId);
        vo.setPointData(pointDataVos);
        return vo;
    }


    /**
     * ------------------PC-------------------------------
     **/

    /**
     * @description: PC获取样线列表
     * @param: [dto]
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.gk.panda.apis.entity.TransectManager.vo.TransectListVo>
     * @date: 2022/11/10 11:41
     * @version: 1.0
     **/
    @Override
    public Page<TransectListVo> getTransectList(TransectListDto dto) {
        //分页查询
        Page<TransectManager> page = new Page<>(dto.getPageNumber(),dto.getPageSize());
        QueryWrapper<TransectManager> wrapper = new QueryWrapper<>();
        wrapper.eq(null != dto.getTransectStatus(), "transect_status", dto.getTransectStatus());
        wrapper.eq("del_flag", Constants.ZERO);
        wrapper.in(!CollectionUtils.isEmpty(dto.getOrgIds()), "fk_org_id", dto.getOrgIds());
        Page<TransectManager> managerPage = baseMapper.selectPage(page, wrapper);
        Page<TransectListVo> voPage = new Page<>();
        if(!CollectionUtils.isEmpty(managerPage.getRecords())){
            List<TransectListVo> vos = managerPage.getRecords().stream().map(transect -> {
                TransectListVo vo = new TransectListVo();
                BeanUtils.copyProperties(transect, vo);
                return vo;
            }).collect(Collectors.toList());
            voPage.setRecords(vos);
            voPage.setPages(managerPage.getPages());
            voPage.setCurrent(managerPage.getCurrent());
            voPage.setSize(managerPage.getSize());
            voPage.setTotal(managerPage.getTotal());
        }
        return voPage;
    }

    @Override
    public Integer addTransect(TransectEntity transectEntity) {
        return transectManagerMapper.insertTransectFile(transectEntity);
    }

    /**
     * @description: PC查看样线详情
     * @param: id
     * @return: com.gk.panda.apis.entity.TransectManager.vo.TransectFileVo
     * @date: 2022/11/10 11:42
     * @version: 1.0
     **/
    @Override
    public TransectFileVo getTransectDetails(Integer id) {
        if(null == id){
            throw new ServerException("样线id不能为空");
        }
      return  transectManagerMapper.getTransectFileInfo(id);
    }

    /**
     * @description: PC删除样线
     * @param: [transectId]
     * @return: java.lang.Integer
     * @date: 2022/11/10 11:42
     * @version: 1.0
     **/
    @Override
    @Transactional
    public Integer removeTransect(Integer transectId) {
        if(null == transectId){
            throw new ServerException("样线id不能为空");
        }
        return transectManagerMapper.removeTransectById(transectId);
    }

    /**
     * @description: 启用禁用样线
     * @param: [transectId]
     * @return: java.lang.Integer
     * @date: 2022/11/10 16:15
     * @version: 1.0
     **/
    @Override
    @Transactional
    public Integer enableDisable(Integer transectId) throws UnsupportedEncodingException {
        if(null == transectId){
            throw new ServerException("样线id不能为空");
        }
        TransectManager manager = baseMapper.selectById(transectId);
        TransectManager transectManager = setTransectManagerValue(transectId);
        if(Constants.ZERO.equals(manager.getTransectStatus())){
            transectManager.setTransectStatus(Constants.ONE);
        } else{
            transectManager.setTransectStatus(Constants.ZERO);
        }
        return baseMapper.updateById(transectManager);
    }

    @Override
    public List<TransectFileVo> getTransectList(TransectFileDto transectFileDto) {
        return transectManagerMapper.getTransectFileList(transectFileDto);
    }

    private TransectManager setTransectManagerValue(Integer transectId) throws UnsupportedEncodingException {
        TransectManager transect = new TransectManager();
        transect.setUpdateTime(new Date());
        transect.setUpdateName(UserUtil.getCurrentUserName());
        transect.setFkUpdate(UserUtil.getCurrentUser());
        transect.setTransectId(transectId);
        return transect;
    }

    /**
     * @description: 根据机构id统计样线数据
     * @param: [orgId]
     * @return: java.lang.Integer
     * @date: 2022/11/11 15:35
     * @version: 1.0
     *
     * @param orgIds*/
    @Override
    public Integer getCountByOrgId(List<Integer> orgIds) {
//        QueryWrapper<TransectManager> wrapper = new QueryWrapper<>();
//        wrapper.eq("del_flag", Constants.ZERO);
//        wrapper.in(!CollectionUtils.isEmpty(orgIds), "fk_org_id", orgIds);
//        return baseMapper.selectCount(wrapper).intValue();
        return baseMapper.getCountByOrgId(orgIds);
    }

    /**
     * 获取机构最后一个自动生成的编号
     * @param fkOrgId
     * @param abbr
     * @return
     */
    @Override
    public String getLastSnByOrgId(Integer fkOrgId, String abbr) {
        return baseMapper.getLastSnByOrgId(fkOrgId,abbr);
    }
}
